package com.nuatar.meibi.Thread;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.nuatar.meibi.Thread.MultiDownloader.ThreadListener;

import android.util.Log;

public class DownloadThread extends Thread {
	int downloadSize = 0;
	int ThreadId;
	ByteArrayOutputStream outstream = new ByteArrayOutputStream();
	int mStart;
	int mLength;
	URL url;
	ThreadListener mTl;
	private boolean finish;

	public DownloadThread(ThreadListener tl, URL urls, int Start, int length,
			int tid) {
		mTl = tl;
		url = urls;
		mStart = Start;
		ThreadId = tid;
		mLength = length;
	}

	@Override
	public void run() {
		try {
			int byteread = 0;
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			http.setConnectTimeout(5 * 1000);
			http.setRequestMethod("GET");
			http.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
			http.setRequestProperty("Accept-Language", "zh-CN");
			http.setRequestProperty("Referer", url.toString()); 
			http.setRequestProperty("Charset", "UTF-8");
			
			int startPos = mStart;
			int endPos = mStart+mLength-1;
			http.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);//���û�ȡʵ����ݵķ�Χ
			http.setRequestProperty("User-Agent", "baidu-bcs-java-sdk/1");
			http.setRequestProperty("Connection", "Keep-Alive");
			InputStream inStream = http.getInputStream();
			byte[] buffer = new byte[1204];
			while ((byteread = inStream.read(buffer)) != -1) {
				downloadSize += byteread;
				outstream.write(buffer, 0, byteread);
				mTl.ReportProgress(downloadSize, ThreadId);
					if(downloadSize==mLength){
						this.finish = true;
				}
			}
			inStream.close();
			mTl.DownLoadResult(true,ThreadId);
		} catch (Exception e) {
			if(e instanceof java.net.SocketException){
				this.start();
				Log.i("THREAD ER","Restart");
			}else{
				mTl.ErrorLog(e,ThreadId);
			}
			
		}
	}

	public int getDownloadSize() {
		return downloadSize;
	}

	public void setDownloadSize(int downloadSize) {
		this.downloadSize = downloadSize;
	}

	public int getThreadId() {
		return ThreadId;
	}

	public void setThreadId(int threadId) {
		ThreadId = threadId;
	}

	public ByteArrayOutputStream getOutstream() {
		return outstream;
	}

	public void setOutstream(ByteArrayOutputStream outstream) {
		this.outstream = outstream;
	}

	public int getmStart() {
		return mStart;
	}

	public void setmStart(int mStart) {
		this.mStart = mStart;
	}

	public int getmLength() {
		return mLength;
	}

	public void setmLength(int mLength) {
		this.mLength = mLength;
	}

	public URL getUrl() {
		return url;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public ThreadListener getmTl() {
		return mTl;
	}

	public void setmTl(ThreadListener mTl) {
		this.mTl = mTl;
	}

	public boolean isFinish() {
		return finish;
	}

	public void setFinish(boolean finish) {
		this.finish = finish;
	}
}
